home *** CD-ROM | disk | FTP | other *** search
/ Creating Your Own America Online Web Pages / Creating Your Own America Online Web Pages.iso / TOOLS / TEX2RTF / SOURCES.ZIP / SRC / WXWIN / WB_HASH.CC < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-03  |  6.9 KB  |  325 lines

  1. /*
  2.  * File:     wx_hash.cc
  3.  * Purpose:  Hash table implementation
  4.  *
  5.  *                       wxWindows 1.50
  6.  * Copyright (c) 1993 Artificial Intelligence Applications Institute,
  7.  *                   The University of Edinburgh
  8.  *
  9.  *                     Author: Julian Smart
  10.  *                       Date: 19-4-93
  11.  *
  12.  * Permission to use, copy, modify, and distribute this software and its
  13.  * documentation for any purpose is hereby granted without fee, provided
  14.  * that the above copyright notice, author statement and this permission
  15.  * notice appear in all copies of this software and related documentation.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS,
  18.  * IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
  19.  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  * IN NO EVENT SHALL THE ARTIFICIAL INTELLIGENCE APPLICATIONS INSTITUTE OR THE
  22.  * UNIVERSITY OF EDINBURGH BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR
  23.  * CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM
  24.  * LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF
  25.  * DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH
  26.  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  */
  28.  
  29. #include "wx_hash.h"
  30.  
  31. #include <iostream.h>
  32. #include <string.h>
  33. #include <stdarg.h>
  34.  
  35. wxHashTable::wxHashTable(unsigned int the_key_type, int size)
  36. {
  37.   __type = wxTYPE_HASH_TABLE;
  38.   n = size;
  39.   current_position = -1;
  40.   current_node = NULL;
  41.  
  42.   key_type = wxKEY_NONE;
  43.   hash_table = new wxList *[size];
  44.   for (int i = 0; i < size; i++)
  45.   {
  46.     hash_table[i] = NULL;
  47.   }
  48. }
  49.  
  50. wxHashTable::~wxHashTable(void)
  51. {
  52.   for (int i = 0; i < n; i++)
  53.     if (hash_table[i])
  54.       delete hash_table[i];
  55.   delete hash_table;
  56. }
  57.  
  58. void wxHashTable::Put(long key, long value, wxObject *object)
  59. {
  60.   if (key < 0) key = -key;
  61.   
  62.   long position = key % n;
  63.   if (!hash_table[position])
  64.     hash_table[position] = new wxList(wxKEY_INTEGER);
  65.  
  66.   hash_table[position]->Append(value, object);
  67. }
  68.  
  69. void wxHashTable::Put(long key, char *value, wxObject *object)
  70. {
  71.   if (key < 0) key = -key;
  72.   
  73.   long position = key % n;
  74.   if (!hash_table[position])
  75.     hash_table[position] = new wxList(wxKEY_INTEGER);
  76.  
  77.   hash_table[position]->Append(value, object);
  78. }
  79.  
  80. void wxHashTable::Put(long key, wxObject *object)
  81. {
  82.   if (key < 0) key = -key;
  83.  
  84.   long position = key % n;
  85.   if (!hash_table[position])
  86.     hash_table[position] = new wxList(wxKEY_INTEGER);
  87.  
  88.   hash_table[position]->Append(key, object);
  89. }
  90.  
  91. void wxHashTable::Put(char *key, wxObject *object)
  92. {
  93.   long int_key = 0;
  94.   int len = strlen(key);
  95.   for (int i = 0; i < len; i++)
  96.     int_key += (long)key[i];
  97.  
  98.   long position = int_key % n;
  99.   if (!hash_table[position])
  100.     hash_table[position] = new wxList(wxKEY_INTEGER);
  101.  
  102.   hash_table[position]->Append(key, object);
  103. }
  104.  
  105. wxObject *wxHashTable::Get(long key, long value)
  106. {
  107.   if (key < 0) key = -key;
  108.  
  109.   long position = key % n;
  110.   if (!hash_table[position])
  111.     return NULL;
  112.   else
  113.   {
  114.     wxNode *node = hash_table[position]->Find(value);
  115.     if (node)
  116.       return node->Data();
  117.     else return NULL;
  118.   }
  119. }
  120.  
  121. wxObject *wxHashTable::Get(long key, char *value)
  122. {
  123.   if (key < 0) key = -key;
  124.  
  125.   long position = key % n;
  126.   if (!hash_table[position])
  127.     return NULL;
  128.   else
  129.   {
  130.     wxNode *node = hash_table[position]->Find(value);
  131.     if (node)
  132.       return node->Data();
  133.     else return NULL;
  134.   }
  135. }
  136.  
  137. wxObject *wxHashTable::Get(long key)
  138. {
  139.   if (key < 0) key = -key;
  140.  
  141.   long position = key % n;
  142.   if (!hash_table[position])
  143.     return NULL;
  144.   else
  145.   {
  146.     wxNode *node = hash_table[position]->Find(key);
  147.     if (node)
  148.       return node->Data();
  149.     else return NULL;
  150.   }
  151. }
  152.  
  153. wxObject *wxHashTable::Get(char *key)
  154. {
  155.   long int_key = 0;
  156.   int len = strlen(key);
  157.   for (int i = 0; i < len; i++)
  158.     int_key += (long)key[i];
  159.  
  160.   long position = int_key % n;
  161.   if (!hash_table[position])
  162.     return NULL;
  163.   else
  164.   {
  165.     wxNode *node = hash_table[position]->Find(key);
  166.     if (node)
  167.       return node->Data();
  168.     else return NULL;
  169.   }
  170. }
  171.  
  172. wxObject *wxHashTable::Delete(long key)
  173. {
  174.   if (key < 0) key = -key;
  175.  
  176.   long position = key % n;
  177.   if (!hash_table[position])
  178.     return NULL;
  179.   else
  180.   {
  181.     wxNode *node = hash_table[position]->Find(key);
  182.     if (node)
  183.     {
  184.       wxObject *data = node->Data();
  185.       delete node;
  186.       return data;
  187.     }
  188.     else return NULL;
  189.   }
  190. }
  191.  
  192. wxObject *wxHashTable::Delete(char *key)
  193. {
  194.   long int_key = 0;
  195.   int len = strlen(key);
  196.   for (int i = 0; i < len; i++)
  197.     int_key += (long)key[i];
  198.  
  199.   long position = int_key % n;
  200.   if (!hash_table[position])
  201.     return NULL;
  202.   else
  203.   {
  204.     wxNode *node = hash_table[position]->Find(key);
  205.     if (node)
  206.     {
  207.       wxObject *data = node->Data();
  208.       delete node;
  209.       return data;
  210.     }
  211.     else return NULL;
  212.   }
  213. }
  214.  
  215. wxObject *wxHashTable::Delete(long key, int value)
  216. {
  217.   if (key < 0) key = -key;
  218.   
  219.   long position = key % n;
  220.   if (!hash_table[position])
  221.     return NULL;
  222.   else
  223.   {
  224.     wxNode *node = hash_table[position]->Find(value);
  225.     if (node)
  226.     {
  227.       wxObject *data = node->Data();
  228.       delete node;
  229.       return data;
  230.     }
  231.     else return NULL;
  232.   }
  233. }
  234.  
  235. wxObject *wxHashTable::Delete(long key, char *value)
  236. {
  237.   if (key < 0) key = -key;
  238.   
  239.   long position = key % n;
  240.   if (!hash_table[position])
  241.     return NULL;
  242.   else
  243.   {
  244.     wxNode *node = hash_table[position]->Find(value);
  245.     if (node)
  246.     {
  247.       wxObject *data = node->Data();
  248.       delete node;
  249.       return data;
  250.     }
  251.     else return NULL;
  252.   }
  253. }
  254.  
  255. long wxHashTable::MakeKey(char *string)
  256. {
  257.   long int_key = 0;
  258.   int len = strlen(string);
  259.   for (int i = 0; i < len; i++)
  260.     int_key += (long)string[i];
  261.  
  262.   if (int_key < 0) int_key = -int_key;
  263.   
  264.   return int_key;
  265. }
  266.  
  267. void wxHashTable::BeginFind(void)
  268. {
  269.   current_position = -1;
  270.   current_node = NULL;
  271. }
  272.  
  273. wxNode *wxHashTable::Next(void)
  274. {
  275.   wxNode *found = NULL;
  276.   Bool end = FALSE;
  277.   while (!end && !found)
  278.   {
  279.     if (!current_node)
  280.     {
  281.       current_position ++;
  282.       if (current_position >= n)
  283.       {
  284.         current_position = -1;
  285.         current_node = NULL;
  286.         end = TRUE;
  287.       }
  288.       else
  289.       {
  290.         if (hash_table[current_position])
  291.         {
  292.           current_node = hash_table[current_position]->First();
  293.           found = current_node;
  294.         }
  295.       }
  296.     }
  297.     else
  298.     {
  299.       current_node = current_node->Next();
  300.       found = current_node;
  301.     }
  302.   }
  303.   return found;
  304. }
  305.  
  306. void wxHashTable::DeleteContents(Bool flag)
  307. {
  308.   for (int i = 0; i < n; i++)
  309.   {
  310.     if (hash_table[i])
  311.       hash_table[i]->DeleteContents(flag);
  312.   }
  313. }
  314.  
  315. void wxHashTable::Clear(void)
  316. {
  317.   for (int i = 0; i < n; i++)
  318.   {
  319.     if (hash_table[i])
  320.       hash_table[i]->Clear();
  321.   }
  322. }
  323.  
  324.  
  325.